﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Entity;

namespace Project_V1
{
    public partial class FrmLayerStack : Form
    {
        private LayerStackService objLayerStackService = new LayerStackService();
        private LayerStack objLayerStack;
        public FrmLayerStack()
        {
            InitializeComponent();
            this.Load += FrmLayerStack_Load;
        }

        private void FrmLayerStack_Load(object sender, EventArgs e)
        {
            //CommonMethods.LayerStackList = objLayerStackService.GetAll();//已经在主界面读取过集合
            this.dgv_data.AutoGenerateColumns = false;//只显示自定义的列
            this.dgv_data.DataSource = CommonMethods.LayerStackList;
            foreach (Control item in this.groupBox1.Controls)
            {
                if (item is Button)
                {
                    item.Visible = false;
                }
            }
            UpdateText();
        }

        #region 新增行,需要点击确定\取消
        private void btn_New_Click(object sender, EventArgs e)
        {
            this.groupBox1.Text = "新增界面";
            foreach (Control item in this.groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    item.Enabled = true;
                    item.Text = "";
                }
            }

            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveNew.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveEdit.Visible = false;
        }
        #endregion

        #region 编辑行,需要点击确定\取消,可继续细化文本改变时才可保存
        private void btn_Edit_Click(object sender, EventArgs e)
        {
            this.groupBox1.Text = "编辑界面";
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要编辑的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要编辑的行！", "提示信息");
                return;
            }
            foreach (Control item in this.groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    item.Enabled = true;
                }
            }

            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            this.btn_SaveEdit.Visible = true;
            this.btn_Cancel.Visible = true;
            this.btn_SaveNew.Visible = false;

        }
        #endregion

        #region 删除行,弹出对话框确认是否删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.RowCount == 0)
            {
                MessageBox.Show("没有要删除的行！", "提示信息");
                return;
            }
            if (this.dgv_data.CurrentRow == null)
            {
                MessageBox.Show("请选中要删除的行！", "提示信息");
                return;
            }
            this.dgv_data.Enabled = false;//若点击编辑/新增/删除,DGV不能再选定其他行,不能再次编辑/新增/删除,直到点击确定或取消
            this.btn_Delete.Enabled = false;
            this.btn_New.Enabled = false;
            this.btn_Edit.Enabled = false;

            //弹出对话框,确认是否删除
            string layerStackName = this.dgv_data.CurrentRow.Cells["LayerStackName"].Value.ToString();
            DialogResult result = MessageBox.Show("确认要删除板材组合  [" + layerStackName + "]  吗？", "删除询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                this.dgv_data.Enabled = true;
                this.btn_Delete.Enabled = true;
                this.btn_New.Enabled = true;
                this.btn_Edit.Enabled = true;
                return;
            }

            //获取编号并删除
            string layerStackId = this.dgv_data.CurrentRow.Cells["LayerStackId"].Value.ToString();
            try
            {
                if (objLayerStackService.DeleteLayerStackById(layerStackId) == 1)
                {
                    //btnQuery_Click(null, null);//同步刷新修改的信息
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示信息");
            }

            UpdateDGV();

            this.dgv_data.Enabled = true;
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

        }
        #endregion

        #region 取消按钮
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            UpdateText();

            this.dgv_data.Enabled = true;//点击取消
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }
        #endregion

        //弹出MessageBox(更新成功),更新ID需封装EditId
        private void btn_SaveEdit_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加

            if (this.txt_LayerStackName.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合名称不能为空 ! ", "提示信息");
                this.txt_LayerStackName.Focus();
                return;
            }
            if (this.txt_LayerStackId.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合编号不能为空 ! ", "提示信息");
                this.txt_LayerStackId.Focus();
                return;
            }
            if (this.txt_Material1.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合参数不能为空 ! ", "提示信息");
                this.txt_Material1.Focus();
                return;
            }
            if (this.txt_Material2.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合参数不能为空 ! ", "提示信息");
                this.txt_Material2.Focus();
                return;
            }
            if (this.txt_Thickness1.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合参数不能为空 ! ", "提示信息");
                this.txt_Thickness1.Focus();
                return;
            }
            if (this.txt_Thickness2.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合参数不能为空 ! ", "提示信息");
                this.txt_Thickness2.Focus();
                return;
            }

            //获取选中行的编号
            //this.dgv_data.SelectedRows[0].Cells["LayerStackId"].Value
            int a = Convert.ToInt32(this.dgv_data.CurrentRow.Cells["LayerStackId"].Value);

            //验证编号是否在数据库内已经存在,并且不等于选中行编号
            if (objLayerStackService.IsIdNoExisted(this.txt_LayerStackId.Text.Trim()))
            {
                if (a != Convert.ToInt32(this.txt_LayerStackId.Text.Trim()))
                {
                    MessageBox.Show("板材组合编号不能和现有编号重复！", "验证提示");
                    this.txt_LayerStackId.Focus();
                    this.txt_LayerStackId.SelectAll();
                    return;
                }
            }
            #endregion

            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_LayerStackName.Text.Trim(),
                LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim()),
                Material1 = this.txt_Material1.Text.Trim(),
                Material2 = this.txt_Material2.Text.Trim(),
                EidtId= a
            };

            #endregion

            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objLayerStackService.ModifyLayerStack(objLayerStack) == 1)
                {
                    MessageBox.Show("板材组合参数更新成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("更新失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            UpdateDGV();
            this.dgv_data.Enabled = true;//点击确定,编辑完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        //弹出MessageBox(添加成功)
        private void btn_SaveNew_Click(object sender, EventArgs e)
        {
            #region 数据验证,还需要继续添加

            if (this.txt_LayerStackName.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合名称不能为空 ! ", "提示信息");
                this.txt_LayerStackName.Focus();
                return;
            }
            if (this.txt_LayerStackId.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材组合编号不能为空 ! ", "提示信息");
                this.txt_LayerStackId.Focus();
                return;
            }
            if (this.txt_Material1.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材参数不能为空 ! ", "提示信息");
                this.txt_Material1.Focus();
                return;
            }
            if (this.txt_Material2.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材参数不能为空 ! ", "提示信息");
                this.txt_Material2.Focus();
                return;
            }
            if (this.txt_Thickness1.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材参数不能为空 ! ", "提示信息");
                this.txt_Thickness1.Focus();
                return;
            }
            if (this.txt_Thickness2.Text.Trim().Length == 0)
            {
                MessageBox.Show("板材参数不能为空 ! ", "提示信息");
                this.txt_Thickness2.Focus();
                return;
            }

            //验证编号是否在数据库内已经存在
            if (objLayerStackService.IsIdNoExisted(this.txt_LayerStackId.Text.Trim()))
            {
                MessageBox.Show("板材组合编号不能和现有编号重复！", "验证提示");
                this.txt_LayerStackId.Focus();
                this.txt_LayerStackId.SelectAll();
                return;
            }
            #endregion

            #region 封装板材组合对象
            LayerStack objLayerStack = new LayerStack()
            {
                LayerStackName = this.txt_LayerStackName.Text.Trim(),
                LayerStackId = Convert.ToInt32(this.txt_LayerStackId.Text.Trim()),
                Material1 = Convert.ToString(this.txt_Material1.Text.Trim()),
                Material2 = Convert.ToString(this.txt_Material2.Text.Trim()),
                Thickness1 = Convert.ToSingle(this.txt_Thickness1.Text.Trim()),
                Thickness2 = Convert.ToSingle(this.txt_Thickness2.Text.Trim())                                
            };

            #endregion

            #region 调用后台数据访问方法,将对象信息存入数据库
            try
            {
                if (objLayerStackService.AddLayerStack(objLayerStack) == 1)
                {
                    MessageBox.Show("新板材组合添加成功", "提示信息");
                }
                else
                {
                    MessageBox.Show("添加失败", "提示信息");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            #endregion

            UpdateDGV();
            this.dgv_data.Enabled = true;//点击确定,新增完成
            this.btn_Delete.Enabled = true;
            this.btn_New.Enabled = true;
            this.btn_Edit.Enabled = true;

            this.btn_SaveEdit.Visible = false;
            this.btn_SaveNew.Visible = false;
            this.btn_Cancel.Visible = false;
        }

        #region 被选中行RowEnter事件,得到被选中行的索引值,和FastenerList集合的索引一一对应        
        private void dgv_data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //DGV初始加载和选择非当前行触发RowEnter事件,该事件结束后CurrentRow属性才会被更新
            objLayerStack = new LayerStack();
            int i = e.RowIndex;//索引值按照由小到大,集合由数据库默认按照主键增序存入,索引一一对应
            objLayerStack = CommonMethods.LayerStackList[i];
            //objFastener.EidtId = Convert.ToInt32(this.dgv_data.Rows[i].Cells["FastenerId"].Value);
            UpdateText();
        }
        #endregion

        #region UpdateDGV()从数据库读取信息存入集合,重新绑定刷新DataGridView
        private void UpdateDGV()//从数据库读取集合信息
        {
            //CommonMethods.SettingsList = objSettingsService.GetAll();
            CommonMethods.LayerStackList = objLayerStackService.GetAll();
            //CommonMethods.FastenerList = objFastenerService.GetAll();
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = CommonMethods.LayerStackList;
        }
        #endregion

        #region UpdateText()更新文本等显示        
        private void UpdateText()
        {
            this.groupBox1.Text = "板材组合参数";
            this.txt_LayerStackId.Text = objLayerStack.LayerStackId.ToString();
            this.txt_LayerStackName.Text = objLayerStack.LayerStackName;
            this.txt_Material1.Text = objLayerStack.Material1;
            this.txt_Thickness1.Text = objLayerStack.Thickness1.ToString();
            this.txt_Material2.Text = objLayerStack.Material2;
            this.txt_Thickness2.Text = objLayerStack.Thickness2.ToString();
            foreach (Control item in this.groupBox1.Controls)
            {
                if (item is TextBox)
                {
                    item.Enabled = false;
                }
            }
        }




        #endregion

        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.btn_Edit_Click(null, null);
        }
    }
}
